package com.joe.test_mybatis_plus.controller;

import com.joe.test_mybatis_plus.entity.User;
import com.joe.test_mybatis_plus.mapper.UserMapper;
import com.joe.test_mybatis_plus.utils.GeoHashUtil;
import com.joe.test_mybatis_plus.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description   描述
 * @Author        Joe
 * @Company       Jycx
 * @CreateDate    2021/9/8 16:24
 */
@RestController
@RequestMapping(value = "/range")
@Api(value = "范围接口",description = "范围接口")
public class RangeController {

    @Autowired
    private GeoHashUtil geoHashUtil;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "添加定位到redis",notes = "添加定位到redis")
    @RequestMapping(value = "/insertRange/{key}",method = RequestMethod.POST)
    public Result insertRange(@PathVariable("key") String key , @RequestParam Long id){
        User user = userMapper.selectById(id);
        if (user!=null){
            double precision = user.getLon().doubleValue();
            double dimension = user.getLat().doubleValue();
            Long aLong = geoHashUtil.redisGeoAdd(key, precision, dimension, user.getName());
            return Result.success(aLong);
        }
        return Result.fail();
    }

    @ApiOperation(value = "获取给定位置元素的位置[经纬度]",notes = "获取给定位置元素的位置[经纬度]")
    @RequestMapping(value = "/getRedisGeo/{key}",method = RequestMethod.GET)
    public Result getRedisGeo(@PathVariable("key") String key , @RequestParam Long id){
        if (id!=null){
            User user = userMapper.selectById(id);
            List<Point> points = geoHashUtil.redisGeoGet(key, user.getName());
            return Result.success(points);
        }
        return Result.fail();
    }

    @ApiOperation(value = "两个定位置元素之间的距离[默认:M]",notes = "两个定位置元素之间的距离[默认:M]")
    @RequestMapping(value = "/geoDist/{key}",method = RequestMethod.GET)
    public Result geoDist(@PathVariable("key") String key , @RequestParam Long id1, @RequestParam Long id2){
        if (id1!=null && id2!=null){
            User user1 = userMapper.selectById(id1);
            User user2 = userMapper.selectById(id2);
            Distance distance = geoHashUtil.geoDist(key, user1.getName(), user2.getName());
            return Result.success(distance);
        }
        return Result.fail();
    }

    @ApiOperation(value = "给定经纬度附近距离内的人数",notes = "给定经纬度附近距离内的人数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "precision", required = true, dataType = "double", paramType = "query", value = "经度",defaultValue = "108.9480230"),
            @ApiImplicitParam(name = "dimension", required = true, dataType = "double", paramType = "query", value = "纬度",defaultValue = "34.263160"),
            @ApiImplicitParam(name = "distance", required = true, dataType = "Integer", paramType = "query", value = "距离",defaultValue = "10"),
            @ApiImplicitParam(name = "count", required = true, dataType = "Integer", paramType = "query", value = "人数",defaultValue = "10")
    })
    @RequestMapping(value = "/redisNearByXY/{key}",method = RequestMethod.POST)
    public Result redisNearByXY(@PathVariable("key") String key , @RequestParam double precision,@RequestParam double dimension,@RequestParam Integer distance,@RequestParam Integer count){
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = geoHashUtil.redisNearByXY(key, precision, dimension, distance, count);
        return Result.success(geoResults);
    }

    @ApiOperation(value = "给定名称附近距离内的人数",notes = "给定名称附近距离内的人数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", required = true, dataType = "String", paramType = "query", value = "名称",defaultValue = "陆新红"),
            @ApiImplicitParam(name = "distance", required = true, dataType = "Integer", paramType = "query", value = "距离",defaultValue = "10"),
            @ApiImplicitParam(name = "count", required = true, dataType = "Integer", paramType = "query", value = "人数",defaultValue = "10")
    })
    @RequestMapping(value = "/geoNearByPlace/{key}",method = RequestMethod.POST)
    public Result geoNearByPlace(@PathVariable("key") String key , @RequestParam String name,@RequestParam Integer distance,@RequestParam Integer count){
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = geoHashUtil.geoNearByPlace(key, name, distance, count);
        return Result.success(geoResults);
    }

    @ApiOperation(value = "返回一个或多个位置元素的 Geohash",notes = "返回一个或多个位置元素的 Geohash")
    @RequestMapping(value = "/geoHash/{key}",method = RequestMethod.POST)
    public Result geoHash(@PathVariable("key") String key, @RequestParam Long id){
        if (id!=null){
            User user = userMapper.selectById(id);
            List<String> list1 = geoHashUtil.geoHash(key, user.getName());
            return Result.success(list1);
        }
        return Result.fail();

    }
}
